{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing Packages\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from math import comb\n",
    "import pickle\n",
    "import math\n",
    "import copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Parameters\n",
    "v=10\n",
    "lam=.125\n",
    "mu=100000\n",
    "c=1\n",
    "\n",
    "start=1\n",
    "end=1001\n",
    "inc=float(1)/(end-start)\n",
    "\n",
    "tolerance=.01\n",
    "endow=10\n",
    "\n",
    "lamb=1\n",
    "N=2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Importing**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "guessF=[[1 for i in range(0,end)] for j in range(10)] \n",
    "guessL=[[1 for i in range(0,end)] for j in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for period in range(1,11):\n",
    "    guessF[period-1]=pickle.load(open(\"../Two-Person Leaderboard Feedback/Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n",
    "    guessL[period-1]=pickle.load(open(\"../Two-Person Leaderboard Feedback/Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Last Period**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def finalTL(FS,draws,follower_probs):\n",
    "    \n",
    "#     print(math.ceil(FS*100))\n",
    "    \n",
    "    pf=follower_probs[math.ceil(FS*1000)]\n",
    "\n",
    "    vNoDrawL=(((1-pf)+(pf*FS))*(v+endow-draws*(c)))+(1-((1-pf)+(pf*FS)))*(endow-draws*(lamb))\n",
    "    vDrawL=(v+endow-(draws+1)*((c)))*(pf*(((FS**2))+(FS*(1-FS))+(float(((1-FS)**2))/2))+(1-pf))+(endow-(draws+1)*((lamb)))*(1-(pf*(((FS**2))+(FS*(1-FS))+(float(((1-FS)**2))/2))+(1-pf)))\n",
    "    pl= np.divide(float(np.exp(mu*vDrawL-max(mu*vDrawL,mu*vNoDrawL))),(np.exp(mu*vDrawL-max(mu*vDrawL,mu*vNoDrawL))+np.exp(mu*vNoDrawL-max(mu*vDrawL,mu*vNoDrawL))))\n",
    "         \n",
    "    piL=pl*vDrawL + (1-pl)*vNoDrawL\n",
    "        \n",
    "    return pl,piL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def finalTF(FS,draws,leader_probs):\n",
    "\n",
    "    pl=leader_probs[math.ceil(FS*1000)]\n",
    "\n",
    "    vNoDrawF=endow-draws*(lamb*c)\n",
    "    vDrawF=(v+endow-(draws+1)*((c)))*((pl*((FS*(1-FS))+(float(((1-FS)**2))/2)))+(1-pl)*(((1-FS))))+(endow-(draws+1)*((lamb)))*(1-((pl*((FS*(1-FS))+(float(((1-FS)**2))/2)))+(1-pl)*(((1-FS)))))\n",
    "    pf=np.divide(float(np.exp(mu*vDrawF-max(mu*vDrawF,mu*vNoDrawF))),(np.exp(mu*vDrawF-max(mu*vDrawF,mu*vNoDrawF))+np.exp(mu*vNoDrawF-max(mu*vDrawF,mu*vNoDrawF))))\n",
    "\n",
    "    \n",
    "    piF=pf*vDrawF+(1-pf)*vNoDrawF\n",
    "        \n",
    "    return pf,piF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def final0():\n",
    "    \n",
    "    draws=0\n",
    "    \n",
    "    #initialize guess\n",
    "    p1=.2\n",
    "    old=0\n",
    "\n",
    "    while abs(p1-old)>tolerance:\n",
    "        \n",
    "        #player 2 values based on player 1 \n",
    "        vNoDraw2=(((1-p1)/2)*float(v+endow-draws*(c)))+((1-(1-p1)/2)*float(endow-draws*(lamb)))\n",
    "        vDraw2=(((p1/2)+(1-p1))*(v+endow-(draws+1)*((c))))+(1-((p1/2)+(1-p1)))*(endow-(draws+1)*((lamb)))\n",
    "        p2= np.divide(float(np.exp(mu*vDraw2-max(mu*vDraw2,mu*vNoDraw2))),(np.exp(mu*vDraw2-max(mu*vDraw2,mu*vNoDraw2))+np.exp(mu*vNoDraw2-max(mu*vDraw2,mu*vNoDraw2))))\n",
    "        \n",
    "        old=p1\n",
    "        \n",
    "        vNoDraw1=(((1-p2)/2)*float(v+endow-draws*(c)))+((1-(1-p2)/2)*float(endow-draws*(lamb)))\n",
    "        vDraw1=(((p2/2)+(1-p2))*(v+endow-(draws+1)*((c))))+(1-((p2/2)+(1-p2)))*(endow-(draws+1)*((lamb)))\n",
    "        p1= np.divide(float(np.exp(mu*vDraw1-max(mu*vDraw1,mu*vNoDraw1))),(np.exp(mu*vDraw1-max(mu*vDraw1,mu*vNoDraw1))+np.exp(mu*vNoDraw1-max(mu*vDraw1,mu*vNoDraw1))))\n",
    "        \n",
    "    pi1=p1*vDraw1+(1-p1)*vNoDraw1\n",
    "    pi2=p2*vDraw2 + (1-p2)*vNoDraw2\n",
    "        \n",
    "    return p1,p2,pi1,pi2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Period 9**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def intermediateF(FS,payoffsL,payoffsF,draws,leader_probs,expected_payoffs):\n",
    "    \n",
    "    pl=leader_probs[math.ceil(FS*1000)]\n",
    "    \n",
    "    [expectF1_ND,expectF2_ND,expectL1_ND,expectL2_ND,expectF1_D,expectF2_D,expectL1_D,expectL2_D,piFN_ND,piFL_ND,piFN_D,piFL_D]=expected_payoffs\n",
    "\n",
    "\n",
    "    vDrawF=(1-pl)*((FS*piFN_D)+(1-FS)*expectL1_D) \n",
    "    vDrawF=vDrawF + pl*(((FS**2)*piFN_D+FS*(1-FS)*expectF1_D)+(float((1-FS)**2)/2)*expectF2_D + (float((1-FS)**2)/2)*expectL2_D + FS*(1-FS)*expectL1_D)\n",
    "    vNoDrawF=(1-pl)*piFN_ND + pl*(FS*piFN_ND + (1-FS)*expectF1_ND)\n",
    "    pf=np.divide(float(np.exp(mu*vDrawF-max(mu*vDrawF,mu*vNoDrawF))),(np.exp(mu*vDrawF-max(mu*vDrawF,mu*vNoDrawF))+np.exp(mu*vNoDrawF-max(mu*vDrawF,mu*vNoDrawF))))\n",
    "\n",
    "\n",
    "    piF=pf*vDrawF+(1-pf)*vNoDrawF\n",
    "        \n",
    "    return [pf,piF]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def intermediateL(FS,payoffsL,payoffsF,draws,follower_probs,expected_payoffs):\n",
    "    \n",
    "    [expectF1_ND,expectF2_ND,expectL1_ND,expectL2_ND,expectF1_D,expectF2_D,expectL1_D,expectL2_D,piFN_ND,piFL_ND,piFN_D,piFL_D]=expected_payoffs\n",
    "\n",
    "#     print(expectF1_ND)\n",
    "    \n",
    "    pf=follower_probs[math.ceil(FS*1000)]  \n",
    "        \n",
    "    vDrawL=(1-pf)*((FS*piFL_D)+(1-FS)*expectL1_D) \n",
    "    vDrawL=vDrawL + pf*(((FS**2)*piFL_D+FS*(1-FS)*expectF1_D)+(float((1-FS)**2)/2)*expectF2_D + (float((1-FS)**2)/2)*expectL2_D + FS*(1-FS)*expectL1_D)\n",
    "    vNoDrawL=(1-pf)*piFL_ND + pf*(FS*piFL_ND + (1-FS)*expectF1_ND)\n",
    "    pl=np.divide(float(np.exp(mu*vDrawL-max(mu*vDrawL,mu*vNoDrawL))),(np.exp(mu*vDrawL-max(mu*vDrawL,mu*vNoDrawL))+np.exp(mu*vNoDrawL-max(mu*vDrawL,mu*vNoDrawL))))\n",
    "\n",
    "    piL=pl*vDrawL+(1-pl)*vNoDrawL\n",
    "        \n",
    "    return [pl,piL]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def intermediate0(payoffsL,payoffsF):\n",
    "    \n",
    "    draws=0\n",
    "    \n",
    "    p1=.2\n",
    "    old=0\n",
    "    \n",
    "    expectF1_ND=sum([((payoffsF[draws][i])*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectF2_ND=sum([((payoffsF[draws][i])*2*(float(.001*i)/(1))*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectL1_ND=sum([((payoffsL[draws][i])*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectL2_ND=sum([((payoffsL[draws][i])*2*(float(.001*i)/(1))*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "\n",
    "    expectF1_D=sum([((payoffsF[draws+1][i])*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectF2_D=sum([((payoffsF[draws+1][i])*2*(float(.001*i)/(1))*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectL1_D=sum([((payoffsL[draws+1][i])*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    expectL2_D=sum([((payoffsL[draws+1][i])*2*(float(.001*i)/(1))*(float(1)/(1))*.001) for i in range(int(math.ceil(0)),1001)])\n",
    "    \n",
    "    \n",
    "    while abs(p1-old)>tolerance:\n",
    "        \n",
    "\n",
    "        vDraw2=(1-p1)*(expectL1_D)+p1*(.5*expectF2_D+.5*expectL2_D)\n",
    "        vNoDraw2=(1-p1)*(payoffsL[draws][0])+p1*expectF1_ND\n",
    "        p2=np.divide(float(np.exp(mu*vDraw2-max(mu*vDraw2,mu*vNoDraw2))),(np.exp(mu*vDraw2-max(mu*vDraw2,mu*vNoDraw2))+np.exp(mu*vNoDraw2-max(mu*vDraw2,mu*vNoDraw2))))\n",
    "        \n",
    "        old=p1\n",
    "        \n",
    "        vDraw1=(1-p2)*(expectL1_D)+p2*(.5*expectF2_D+.5*expectL2_D)\n",
    "        vNoDraw1=(1-p2)*(payoffsF[draws][0])+p2*expectF1_ND\n",
    "        p1=np.divide(float(np.exp(mu*vDraw1-max(mu*vDraw1,mu*vNoDraw1))),(np.exp(mu*vDraw1-max(mu*vDraw1,mu*vNoDraw1))+np.exp(mu*vNoDraw1-max(mu*vDraw1,mu*vNoDraw1))))\n",
    "        \n",
    "        pi1=p1*vDraw1+(1-p1)*vNoDraw1\n",
    "        pi2=p2*vDraw2+(1-p2)*vNoDraw2\n",
    "        \n",
    "    return [p1,p2,pi1,pi2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Epayoffs(FS,payoffsL,payoffsF,draws):\n",
    "    #create expectations for next period to use in the functions\n",
    "    \n",
    "#     print(FS)\n",
    "    if math.ceil(FS*1000)<1000:\n",
    "       \n",
    "        expectF1_ND=sum([((payoffsF[draws][i])*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectF2_ND=sum([((payoffsF[draws][i])*2*(float(.001*i-FS)/(1-FS))*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectL1_ND=sum([((payoffsL[draws][i])*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectL2_ND=sum([((payoffsL[draws][i])*2*(float(.001*i-FS)/(1-FS))*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        \n",
    "        expectF1_D=sum([((payoffsF[draws+1][i])*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectF2_D=sum([((payoffsF[draws+1][i])*2*(float(.001*i-FS)/(1-FS))*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectL1_D=sum([((payoffsL[draws+1][i])*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "        expectL2_D=sum([((payoffsL[draws+1][i])*2*(float(.001*i-FS)/(1-FS))*(float(1)/(1-FS))*.001) for i in range(int(math.ceil(FS*1000)),1001)])\n",
    "    \n",
    "        \n",
    "    \n",
    "    elif math.ceil(FS*1000)==1000:\n",
    "        [expectF1_ND,expectF2_ND,expectL1_ND,expectL2_ND,expectF1_D,expectF2_D,expectL1_D,expectL2_D]=[0,0,0,0,0,0,0,0]\n",
    "\n",
    "    \n",
    "    #calcuate payoffs of going into next period with same score\n",
    "    piFN_ND=payoffsF[draws][math.ceil(FS*1000)]\n",
    "    piFL_ND=payoffsL[draws][math.ceil(FS*1000)]\n",
    "    \n",
    "    piFN_D=payoffsF[draws+1][math.ceil(FS*1000)]\n",
    "    piFL_D=payoffsL[draws+1][math.ceil(FS*1000)]\n",
    "    \n",
    "    \n",
    "    return [expectF1_ND,expectF2_ND,expectL1_ND,expectL2_ND,expectF1_D,expectF2_D,expectL1_D,expectL2_D,piFN_ND,piFL_ND,piFN_D,piFL_D]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def probabilities(guessL,guessF):\n",
    "\n",
    "    f10,f9,f8,f7,f6,f5,f4,f3,f2,f1=[[] for k in range(10)],[[] for k in range(9)],[[] for k in range(8)],[[] for k in range(7)],[[] for k in range(6)],[[] for k in range(5)],[[] for k in range(4)],[[] for k in range(3)],[[] for k in range(2)],[[] for k in range(1)]\n",
    "    l10,l9,l8,l7,l6,l5,l4,l3,l2,l1=[[] for k in range(10)],[[] for k in range(9)],[[] for k in range(8)],[[] for k in range(7)],[[] for k in range(6)],[[] for k in range(5)],[[] for k in range(4)],[[] for k in range(3)],[[] for k in range(2)],[[] for k in range(1)]\n",
    "    pi10F,pi9F,pi8F,pi7F,pi6F,pi5F,pi4F,pi3F,pi2F,pi1F=[[] for k in range(10)],[[] for k in range(9)],[[] for k in range(8)],[[] for k in range(7)],[[] for k in range(6)],[[] for k in range(5)],[[] for k in range(4)],[[] for k in range(3)],[[] for k in range(2)],[[] for k in range(1)]\n",
    "    pi10L,pi9L,pi8L,pi7L,pi6L,pi5L,pi4L,pi3L,pi2L,pi1L=[[] for k in range(10)],[[] for k in range(9)],[[] for k in range(8)],[[] for k in range(7)],[[] for k in range(6)],[[] for k in range(5)],[[] for k in range(4)],[[] for k in range(3)],[[] for k in range(2)],[[] for k in range(1)]\n",
    "\n",
    "\n",
    "    for j in range(10):\n",
    "        \n",
    "        [p1,p2,pi1,pi2]=final0()\n",
    "\n",
    "        f10[j].append(p1)\n",
    "        l10[j].append(p2)\n",
    "\n",
    "        pi10F[j].append(pi2)\n",
    "        pi10L[j].append(pi2)\n",
    "\n",
    "        for i in range(start,end):\n",
    "            [pf,piF]=finalTF(i*inc,j,guessL[9])\n",
    "            [pl,piL]=finalTL(i*inc,j,guessF[9])\n",
    "\n",
    "            f10[j].append(pf)\n",
    "            l10[j].append(pl)\n",
    "            pi10F[j].append(piF)\n",
    "            pi10L[j].append(piL)\n",
    "\n",
    "\n",
    "\n",
    "    for j in range(9):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi10L,pi10F)\n",
    "\n",
    "        f9[j].append(pi)\n",
    "        l9[j].append(po)\n",
    "\n",
    "        pi9F[j].append(pi1)\n",
    "        pi9L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi10L,pi10F,j)\n",
    "            \n",
    "            [pf,piF]=intermediateF(i*inc,pi10L,pi10F,j,guessL[8],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi10L,pi10F,j,guessF[8],expected_payoffs)\n",
    "\n",
    "            f9[j].append(pf)\n",
    "            l9[j].append(pl)\n",
    "            pi9F[j].append(piF)\n",
    "            pi9L[j].append(piL)\n",
    "\n",
    "\n",
    "\n",
    "    for j in range(8):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi9L,pi9F)\n",
    "\n",
    "        f8[j].append(pi)\n",
    "        l8[j].append(po)\n",
    "\n",
    "        pi8F[j].append(pi1)\n",
    "        pi8L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi9L,pi9F,j)\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi9L,pi9F,j,guessL[7],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi9L,pi9F,j,guessF[7],expected_payoffs)\n",
    "\n",
    "            f8[j].append(pf)\n",
    "            l8[j].append(pl)\n",
    "            pi8F[j].append(piF)\n",
    "            pi8L[j].append(piL)\n",
    "\n",
    "\n",
    "\n",
    "    for j in range(7):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi8L,pi8F)\n",
    "\n",
    "        f7[j].append(pi)\n",
    "        l7[j].append(po)\n",
    "\n",
    "        pi7F[j].append(pi1)\n",
    "        pi7L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi8L,pi8F,j)\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi8L,pi8F,j,guessL[6],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi8L,pi8F,j,guessF[6],expected_payoffs)\n",
    "\n",
    "            f7[j].append(pf)\n",
    "            l7[j].append(pl)\n",
    "            pi7F[j].append(piF)\n",
    "            pi7L[j].append(piL)\n",
    "\n",
    "    for j in range(6):\n",
    "        \n",
    "\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi7L,pi7F)\n",
    "\n",
    "        f6[j].append(pi)\n",
    "        l6[j].append(po)\n",
    "\n",
    "        pi6F[j].append(pi1)\n",
    "        pi6L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi7L,pi7F,j)\n",
    "            \n",
    "            [pf,piF]=intermediateF(i*inc,pi7L,pi7F,j,guessL[5],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi7L,pi7F,j,guessF[5],expected_payoffs)\n",
    "\n",
    "            f6[j].append(pf)\n",
    "            l6[j].append(pl)\n",
    "            pi6F[j].append(piF)\n",
    "            pi6L[j].append(piL)\n",
    "\n",
    "    for j in range(5):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi6L,pi6F)\n",
    "\n",
    "        f5[j].append(pi)\n",
    "        l5[j].append(po)\n",
    "\n",
    "        pi5F[j].append(pi1)\n",
    "        pi5L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi6L,pi6F,j)\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi6L,pi6F,j,guessL[4],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi6L,pi6F,j,guessF[4],expected_payoffs)\n",
    "\n",
    "            f5[j].append(pf)\n",
    "            l5[j].append(pl)\n",
    "            pi5F[j].append(piF)\n",
    "            pi5L[j].append(piL)\n",
    "\n",
    "    for j in range(4):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi5L,pi5F)\n",
    "\n",
    "        f4[j].append(pi)\n",
    "        l4[j].append(po)\n",
    "\n",
    "        pi4F[j].append(pi1)\n",
    "        pi4L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi5L,pi5F,j)\n",
    "\n",
    "\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi5L,pi5F,j,guessL[3],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi5L,pi5F,j,guessF[3],expected_payoffs)\n",
    "\n",
    "            f4[j].append(pf)\n",
    "            l4[j].append(pl)\n",
    "            pi4F[j].append(piF)\n",
    "            pi4L[j].append(piL)       \n",
    "\n",
    "    for j in range(3):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi4L,pi4F)\n",
    "\n",
    "        f3[j].append(pi)\n",
    "        l3[j].append(po)\n",
    "\n",
    "        pi3F[j].append(pi1)\n",
    "        pi3L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi4L,pi4F,j)\n",
    "\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi4L,pi4F,j,guessL[2],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi4L,pi4F,j,guessF[2],expected_payoffs)\n",
    "\n",
    "            f3[j].append(pf)\n",
    "            l3[j].append(pl)\n",
    "            pi3F[j].append(piF)\n",
    "            pi3L[j].append(piL) \n",
    "\n",
    "    for j in range(2):\n",
    "\n",
    "        [pi,po,pi1,pi2]=intermediate0(pi3L,pi3F)\n",
    "\n",
    "        f2[j].append(pi)\n",
    "        l2[j].append(po)\n",
    "\n",
    "        pi2F[j].append(pi1)\n",
    "        pi2L[j].append(pi2)\n",
    "\n",
    "\n",
    "        for i in range(start,end):\n",
    "            \n",
    "            expected_payoffs=Epayoffs(i*inc,pi3L,pi3F,j)\n",
    "\n",
    "\n",
    "            [pf,piF]=intermediateF(i*inc,pi3L,pi3F,j,guessL[1],expected_payoffs)\n",
    "            [pl,piL]=intermediateL(i*inc,pi3L,pi3F,j,guessF[1],expected_payoffs)\n",
    "\n",
    "            f2[j].append(pf)\n",
    "            l2[j].append(pl)\n",
    "            pi2F[j].append(piF)\n",
    "            pi2L[j].append(piL) \n",
    "\n",
    "\n",
    "\n",
    "    [pi,po,pi1,pi2]=intermediate0(pi2L,pi2F)\n",
    "\n",
    "    f1[0].append(pi)\n",
    "    l1[0].append(po)\n",
    "\n",
    "    pi1F[0].append(pi1)\n",
    "    pi1L[0].append(pi2)\n",
    "\n",
    "\n",
    "    return([f10,f9,f8,f7,f6,f5,f4,f3,f2,f1,l10,l9,l8,l7,l6,l5,l4,l3,l2,l1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# guessF=[[1 for i in range(0,end)] for j in range(10)] \n",
    "# guessL=[[1 for i in range(0,end)] for j in range(10)]\n",
    "\n",
    "[f10,f9,f8,f7,f6,f5,f4,f3,f2,f1,l10,l9,l8,l7,l6,l5,l4,l3,l2,l1]=probabilities(guessL,guessF)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def inverse(y):\n",
    "    return (-np.log(1-y)*float(1)/.125)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "probF10=f10\n",
    "probF9=f9\n",
    "probF8=f8\n",
    "probF7=f7\n",
    "probF6=f6\n",
    "probF5=f5\n",
    "probF4=f4\n",
    "probF3=f3\n",
    "probF2=f2\n",
    "probF1=f1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "probL10=l10\n",
    "probL9=l9\n",
    "probL8=l8\n",
    "probL7=l7\n",
    "probL6=l6\n",
    "probL5=l5\n",
    "probL4=l4\n",
    "probL3=l3\n",
    "probL2=l2\n",
    "probL1=l1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Fprobs=[probF1,probF2,probF3,probF4,probF5,probF6,probF7,probF8,probF9,probF10]\n",
    "Lprobs=[probL1,probL2,probL3,probL4,probL5,probL6,probL7,probL8,probL9,probL10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Simulation(probL1,probF1,Lprobs,Fprobs):\n",
    "\n",
    "    N=2\n",
    "    T=10\n",
    "    \n",
    "    \n",
    "\n",
    "    fdraws=[[[] for k in range(1001)] for d in range(10)]\n",
    "    ldraws=[[[] for k in range(1001)] for d in range(10)]\n",
    "    farrive=[[0 for k in range(1001)] for d in range(10)]\n",
    "    larrive=[[0 for k in range(1001)] for d in range(10)]\n",
    "\n",
    "    drawslist=[]\n",
    "    scoreslist=[]\n",
    "    for iter in range(100000):\n",
    "        scores=[0 for i in range(N)]\n",
    "        draws=[0 for i in range(N)]\n",
    "        for period in range(1,T+1):\n",
    "            FS=int(math.ceil((1-np.exp(-.125*max(scores)))*1000))\n",
    "            mscore=max(scores)\n",
    "            for player in range(N):\n",
    "                if period==1:\n",
    "                    if np.random.choice([0,1],p=[1-probF1[draws[player]][0],probF1[draws[player]][0]])==1:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                else:\n",
    "                    if leader[player]==1:\n",
    "                        choice=0\n",
    "                        ldraws[period-1][FS].append(draws[player])\n",
    "                        larrive[period-1][FS]=larrive[period-1][FS]+1\n",
    "                        if np.random.choice([0,1],p=[1-Lprobs[period-1][draws[player]][FS],Lprobs[period-1][draws[player]][FS]])==1:\n",
    "                            scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                            draws[player]=draws[player]+1\n",
    "                            choice=1\n",
    "\n",
    "\n",
    "                    else:\n",
    "                        choice=0\n",
    "                        fdraws[period-1][FS].append(draws[player])\n",
    "                        farrive[period-1][FS]=farrive[period-1][FS]+1\n",
    "                        if np.random.choice([0,1],p=[1-Fprobs[period-1][draws[player]][FS],Fprobs[period-1][draws[player]][FS]])==1:\n",
    "                            scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                            draws[player]=draws[player]+1\n",
    "                            choice=1\n",
    "\n",
    "\n",
    "            leader=[0 for i in range(N)]\n",
    "            leader[np.argmax(scores)]=1\n",
    "\n",
    "\n",
    "\n",
    "    #             print(leader)\n",
    "    #     print(draws)\n",
    "        drawslist.append(sum(draws))\n",
    "        scoreslist.append(max(scores))\n",
    "\n",
    "    for j in range(10):\n",
    "        for k in range(0,end):\n",
    "            if (farrive[j][k]>0):\n",
    "                guessF[j][k]=((1-epsilon)*guessF[j][k])+epsilon*sum([Fprobs[j][fdraws[j][k][i]][k] for i in range(len(fdraws[j][k]))])/len(fdraws[j][k])\n",
    "            if (larrive[j][k]>0):\n",
    "                guessL[j][k]=((1-epsilon)*guessL[j][k])+epsilon*sum([Lprobs[j][ldraws[j][k][i]][k] for i in range(len(ldraws[j][k]))])/len(ldraws[j][k])\n",
    "\n",
    "\n",
    "\n",
    "    print(np.array(drawslist).mean())\n",
    "\n",
    "    return [guessL,guessF,np.array(drawslist).mean()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Iteration**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "draws_per_lamb=[]\n",
    "FinalProbsF=[]\n",
    "FinalProbsL=[]\n",
    "for lamb in [1+i*.05 for i in range(19)]:\n",
    "    epsilon=.1\n",
    "\n",
    "    oldguessF=[[100 for i in range(0,end)] for j in range(10)] \n",
    "    oldguessL=[[100 for i in range(0,end)] for j in range(10)]\n",
    "\n",
    "\n",
    "    while sum(sum(abs(np.array(guessL)-np.array(oldguessL))))+sum(sum(abs(np.array(guessF)-np.array(oldguessF))))>75:\n",
    "\n",
    "    #     if i>0:\n",
    "\n",
    "        [f10,f9,f8,f7,f6,f5,f4,f3,f2,f1,l10,l9,l8,l7,l6,l5,l4,l3,l2,l1]=probabilities(guessL,guessF)\n",
    "\n",
    "        oldguessL=copy.deepcopy(guessL)\n",
    "        oldguessF=copy.deepcopy(guessF)\n",
    "\n",
    "\n",
    "        probF1=f1\n",
    "\n",
    "        probL1=l1\n",
    "\n",
    "\n",
    "        Fprobs=[probF1,f2,f3,f4,f5,f6,f7,f8,f9,f10]\n",
    "        Lprobs=[probL1,l2,l3,l4,l5,l6,l7,l8,l9,l10]\n",
    "\n",
    "        [guessL,guessF,contest_results]=Simulation(probF1,probL1,Lprobs,Fprobs)\n",
    "\n",
    "        print(sum(sum(abs(np.array(guessL)-np.array(oldguessL))))+sum(sum(abs(np.array(guessF)-np.array(oldguessF)))))\n",
    "    draws_per_lamb.append(contest_results)    \n",
    "    FinalProbsF.append(Fprobs)\n",
    "    FinalProbsL.append(Lprobs) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('Public_Loss.pkl', 'wb') as f:\n",
    "    pickle.dump(draws_per_lamb, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation For Each Factor**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SimulationDataset(probL1,probF1,Lprobs,Fprobs):\n",
    "\n",
    "    N=2\n",
    "    T=10\n",
    "    \n",
    "    drawslist=[]\n",
    "    for iter in range(10000):\n",
    "        scores=[0 for i in range(N)]\n",
    "        draws=[0 for i in range(N)]\n",
    "        for period in range(1,T+1):\n",
    "            FS=int(math.ceil((1-np.exp(-.125*max(scores)))*1000))\n",
    "            mscore=max(scores)\n",
    "            for player in range(N):\n",
    "                if period==1:\n",
    "                    if np.random.choice([0,1],p=[1-probF1[draws[player]][0],probF1[draws[player]][0]])==1:\n",
    "                        scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                        draws[player]=draws[player]+1\n",
    "                else:\n",
    "                    if leader[player]==1:\n",
    "                        choice=0\n",
    "                        if np.random.choice([0,1],p=[1-Lprobs[period-1][draws[player]][FS],Lprobs[period-1][draws[player]][FS]])==1:\n",
    "                            scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                            draws[player]=draws[player]+1\n",
    "                            choice=1\n",
    "\n",
    "\n",
    "                    else:\n",
    "                        choice=0\n",
    "                        if np.random.choice([0,1],p=[1-Fprobs[period-1][draws[player]][FS],Fprobs[period-1][draws[player]][FS]])==1:\n",
    "                            scores[player]=max(scores[player],np.random.exponential(8))\n",
    "                            draws[player]=draws[player]+1\n",
    "                            choice=1\n",
    "\n",
    "\n",
    "            leader=[0 for i in range(N)]\n",
    "            leader[np.argmax(scores)]=1\n",
    "\n",
    "\n",
    "\n",
    "    #             print(leader)\n",
    "    #     print(draws)\n",
    "        drawslist.append(sum(draws))\n",
    "\n",
    "    return drawslist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Public_Loss_Data=pd.DataFrame(columns=[\"Simulation Number\",\"Factor\",\"Coefficient Value\",\"Number of Draws\",\"Treatment\"])\n",
    "\n",
    "N=2\n",
    "T=10\n",
    "\n",
    "location=0\n",
    "for loss in range(19):\n",
    "    \n",
    "    if loss==0:\n",
    "        \n",
    "        Fprobs=[[[1 for i in range(0,end)] for j in range(x)] for x in range(1,11)]\n",
    "        Lprobs=[[[1 for i in range(0,end)] for j in range(x)] for x in range(1,11)]\n",
    "        \n",
    "        for period in range(1,11):\n",
    "            Fprobs[period-1]=[pickle.load(open(\"../Two-Person Leaderboard Feedback/Alternative_Follower_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\")) for j in range(period)]\n",
    "            Lprobs[period-1]=[pickle.load(open(\"../Two-Person Leaderboard Feedback/Alternative_Leader_Prob_Period_%s_%s_%s_%s.pkl\"%(period,N,v,c),\"rb\")) for j in range(period)]\n",
    "\n",
    "    \n",
    "        probF1=Fprobs[0]\n",
    "        probL1=Lprobs[0]\n",
    "        \n",
    "        \n",
    "        \n",
    "    else:\n",
    "        Fprobs=FinalProbsF[loss]\n",
    "        Lprobs=FinalProbsL[loss]\n",
    "        \n",
    "        probF1=Fprobs[0]\n",
    "        probL1=Lprobs[0]\n",
    "        \n",
    "    contest_draws=SimulationDataset(probF1,probL1,Lprobs,Fprobs)\n",
    "            \n",
    "    for contest in range(len(contest_draws)):\n",
    "        Public_Loss_Data.loc[location]=[iter,\"Loss Aversion Coefficient\",.05*loss,contest_draws[contest],\"Leader-board\"]\n",
    "        location=location+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Public_Loss_Data.to_csv(\"Public_Loss_Data.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
